package com.zcp.todayheadline.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.lianghanzhen.LazyFragmentPagerAdapter;
import com.github.lianghanzhen.LazyViewPager;
import com.headline.pagerslidingtabstrip.PagerSlidingTabStrip;
import com.zcp.todayheadline.R;

public class NewsFragment extends Fragment {

    PagerSlidingTabStrip ps;
    LazyViewPager lazy;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public NewsFragment() {
        // Required empty public constructor
    }


    public static NewsFragment newInstance(String param1, String param2) {

        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_news, container, false);

        //初始化
        initView(view);
        return view;
    }

    //初始化
    public void initView(View view) {
        ps = (PagerSlidingTabStrip) view.findViewById(R.id.ps);
        lazy = (LazyViewPager) view.findViewById(R.id.lazy);

        //设置适配器
        lazy.setAdapter(new NewsFragmentAdapter(getActivity().getSupportFragmentManager()));

        //实现联动效果
        ps.setViewPager(lazy);

        //设置滑动颜色
        ps.setTextColor(Color.RED);
        ps.setTextSize(48);
        ps.setIndicatorColor(Color.BLUE);
        ps.setIndicatorHeight(2);
    }

    //设置适配器
    class NewsFragmentAdapter extends LazyFragmentPagerAdapter {
        String[] titles = new String[]{"足球", "娱乐", "体育", "财经", "科技", "电影", "汽车", "笑话", "游戏", "时尚", "情感", "精选", "电台", "nba"};

        String[] ids = new String[]{
                "T1399700447917", "T1348648517839", "T1348649079062",
                "T1348648756099", "T1348649580692", "T1348648650048",
                "T1348654060988", "T1350383429665", "T1348654151579",
                "T1348650593803", "T1348650839000", "T1370583240249",
                "T1379038288239", "T1348649145984"};

        public NewsFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        protected Fragment getItem(ViewGroup container, int position) {
            return InfoFragment.newInstance(titles[position], ids[position]);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

}
